using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum SelectableEffectType 
{
    Projector = 1,
    HealthBar = 2,
    Outline = 4,
}

public class SelectableEffect : MonoBehaviour
{
    [EnumMaskField("SelectableEffectType")]
    public SelectableEffectType selectableEffectType;

    public HealthBar HealthBarPrefab; 
    public ProjectorEffect ProjectorPrefab;

    private List<int> effects;

    public void OnSelect(SelectableObject selectableObject) 
    {
        effects = MyLayerMask.GetContainLayer((int)selectableEffectType, Enum.GetNames(typeof(SelectableEffectType)).Length);
        for (int i = 0; i < effects.Count; i++) 
        {
            switch ((SelectableEffectType)effects[i])
            {
                case SelectableEffectType.HealthBar:
                    HealthBarEffect(selectableObject);
                    break;
                case SelectableEffectType.Outline:
                    OutlineEffect(selectableObject);
                    break;
                case SelectableEffectType.Projector:
                    ProjectorEffect(selectableObject);
                    break;
            }
        }
        
    }

    public void OnUnSelect(SelectableObject selectableObject)
    {
        for (int i = 0; i < effects.Count; i++)
        {
            switch ((SelectableEffectType)effects[i])
            {
                case SelectableEffectType.HealthBar:
                    ObjectPool.Instance.Close(HealthBarPrefab.gameObject.GetInstanceID());
                    break;
                case SelectableEffectType.Outline:
                    HighlightableObject outline = selectableObject.GetComponent<HighlightableObject>();
                    if (outline != null)
                    {
                        outline.ConstantOff();
                    }
                    break;
                case SelectableEffectType.Projector:
                    ObjectPool.Instance.Close(ProjectorPrefab.gameObject.GetInstanceID());
                    break;
            }
        }
    }

    private void HealthBarEffect(SelectableObject selectableObject) 
    {
        HealthBar healthBar = ObjectPool.Instance.Create<HealthBar>(HealthBarPrefab);
        healthBar.SetFollower(selectableObject);
    }

    private void OutlineEffect(SelectableObject selectableObject) 
    {
        HighlightableObject outline = selectableObject.GetComponent<HighlightableObject>();
        if (outline != null)
        {
            outline.ConstantOn(selectableObject.outlineColor);
        }
    }

    private void ProjectorEffect(SelectableObject selectableObject) 
    {
        ProjectorEffect projector = ObjectPool.Instance.Create<ProjectorEffect>(ProjectorPrefab);
        projector.SetFollower(selectableObject);
    }
}
